package com.sywb.chuangyebao.utils;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.sywb.chuangyebao.R;

import org.bining.footstone.bean.ImagePath;
import org.bining.footstone.image.transformation.BlurTransformation;
import org.bining.footstone.image.transformation.RoundedCornersTransformation;
import org.bining.footstone.utils.ScreenUtils;

import java.io.File;

/**
 * Glide 图片加载封装
 * Created by Bining.
 */
public class GlideUtils {

    /**
     * 加载图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片Id
     * @param transform 图片裁剪类型
     */
    private static void loadImage(Activity activity, ImagePath imagePath, ImageView imageView, @DrawableRes int resId, int transform) {
        loadImage(activity, imagePath, imageView, resId, ScreenUtils.dp2px(3), transform, false);
    }

    /**
     * 加载图片
     *
     * @param imagePathUrl ImageUrl
     * @param imageView    ImageView控件
     * @param resId        默认图片Id
     * @param radius       指定圆角
     * @param transform    图片裁剪类型
     * @param isThumbnail  是否显示缩略图
     */
    private static void loadImage(Activity activity, String imagePathUrl, ImageView imageView, int resId, int radius, int transform, boolean isThumbnail) {
        ImagePath imagePath = new ImagePath();
        imagePath.imagePath = imagePathUrl;
        loadImage(activity, imagePath, imageView, resId, radius, transform, isThumbnail);
    }

    /**
     * 加载图片
     *
     * @param imagePath   ImagePath对象
     * @param imageView   ImageView控件
     * @param resId       默认图片Id
     * @param radius      指定圆角
     * @param transform   图片裁剪类型
     * @param isThumbnail 是否显示缩略图
     */
    private static void loadImage(Activity activity, ImagePath imagePath, ImageView imageView, int resId, int radius, int transform, boolean isThumbnail) {
        if (imagePath != null && !imagePath.imageEmpty) {
            RequestOptions requestOptions = new RequestOptions();
            if (resId > 0) {
                requestOptions.placeholder(resId).error(resId).fallback(resId);
            }
            switch (transform) {
                case 1:
                    requestOptions.transform(new RoundedCornersTransformation(radius, 0));
                    break;
                case 2:
                    requestOptions.circleCrop();
                    break;
                case 3:
                    requestOptions.transform(new BlurTransformation(25));
                    break;
                case 4:
                    requestOptions.centerCrop();
                    break;
                case 5:
                    requestOptions.fitCenter();
            }

            RequestBuilder<Drawable> requestBuilder = null;
            if (!TextUtils.isEmpty(imagePath.imageLocalPath)) {
                File file = new File(imagePath.imageLocalPath);
                if (file.exists()) {
                    requestBuilder = Glide.with(activity).load(file);
                }
            }
            if (requestBuilder == null) {
                // glideRequest = Glide.with(activity).using(new ImageSizeUrlLoader(activity)).load(new GlideUrlJion(imagePath.imagePrefix, imagePath.imageSuffix)).asBitmap();
                requestBuilder = Glide.with(activity).load(imagePath.imagePath);
            }

            if (isThumbnail) {
                requestBuilder.thumbnail(0.1f);
            }
            requestBuilder.apply(requestOptions).into(imageView);
        } else {
            imageView.setImageResource(resId);
        }
    }

    /**
     * 从URl地址获取普通图片
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     */
    public static void getNormalIamgeToUrl(Activity activity, String imagePathUrl, ImageView imageView) {
        getNormalIamgeToUrl(activity, imagePathUrl, imageView, R.drawable.image_def);
    }

    /**
     * 从URl地址获取普通图片
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     * @param resId        默认图片路径
     */
    public static void getNormalIamgeToUrl(Activity activity, String imagePathUrl, ImageView imageView, @DrawableRes int resId) {
        if (TextUtils.isEmpty(imagePathUrl) || TextUtils.isEmpty(imagePathUrl.trim())) {
            imageView.setImageResource(resId);
        } else {
            ImagePath imagePath = new ImagePath();
            imagePath.imagePath = imagePathUrl;
            getNormalIamgeToUrl(activity, imagePath, imageView, resId);
        }
    }

    /**
     * 从URl地址获取普通图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片路径
     */
    public static void getNormalIamgeToUrl(Activity activity, ImagePath imagePath, ImageView imageView, @DrawableRes int resId) {
        loadImage(activity, imagePath, imageView, resId, 0);
    }

    /**
     * 从URl地址获取圆形头像
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     */
    public static void getCircleImageToUrl(Activity activity, String imagePathUrl, ImageView imageView) {
        getCircleImageToUrl(activity, imagePathUrl, imageView, R.drawable.head_image);
    }

    /**
     * 从URl地址获取圆形图片
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     * @param resId        默认图片路径
     */
    public static void getCircleImageToUrl(Activity activity, String imagePathUrl, ImageView imageView, @DrawableRes int resId) {
        if (TextUtils.isEmpty(imagePathUrl) || TextUtils.isEmpty(imagePathUrl.trim())) {
            imageView.setImageResource(resId);
        } else {
            ImagePath imagePath = new ImagePath();
            imagePath.imagePath = imagePathUrl;
            getCircleImageToUrl(activity, imagePath, imageView, resId);
        }
    }

    /**
     * 从URl地址获取圆形图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片路径
     */
    public static void getCircleImageToUrl(Activity activity, ImagePath imagePath, ImageView imageView, @DrawableRes int resId) {
        loadImage(activity, imagePath, imageView, resId, 2);
    }

    /**
     * 从URl地址获取圆角图片
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     * @param resId        默认图片路径
     */
    public static void getRoundImageToUrl(Activity activity, String imagePathUrl, ImageView imageView, @DrawableRes int resId) {
        if (TextUtils.isEmpty(imagePathUrl) || TextUtils.isEmpty(imagePathUrl.trim())) {
            imageView.setImageResource(resId);
        } else {
            ImagePath imagePath = new ImagePath();
            imagePath.imagePath = imagePathUrl;
            getRoundImageToUrl(activity, imagePath, imageView, resId);
        }
    }

    /**
     * 从URl地址获取指定圆角的图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片路径
     * @param radius    指定圆角
     */
    public static void getRoundImageToUrl(Activity activity, ImagePath imagePath, ImageView imageView, int radius, @DrawableRes int resId) {
        loadImage(activity, imagePath, imageView, resId, radius, 1, false);
    }

    /**
     * 从URl地址获取圆角图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片路径
     */
    public static void getRoundImageToUrl(Activity activity, ImagePath imagePath, ImageView imageView, @DrawableRes int resId) {
        loadImage(activity, imagePath, imageView, resId, 1);
    }

    /**
     * 从URl地址获取需要缩略图的大型图片
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     * @param resId        默认图片路径
     */
    public static void getScaleNomalImageToUrl(Activity activity, String imagePathUrl, ImageView imageView, @DrawableRes int resId) {
        if (TextUtils.isEmpty(imagePathUrl) || TextUtils.isEmpty(imagePathUrl.trim())) {
            imageView.setImageResource(resId);
        } else {
            ImagePath imagePath = new ImagePath();
            imagePath.imagePath = imagePathUrl;
            getScaleNomalImageToUrl(activity, imagePath, imageView, resId);
        }
    }

    /**
     * 从URl地址获取需要缩略图的大型图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片路径
     */
    public static void getScaleNomalImageToUrl(Activity activity, ImagePath imagePath, ImageView imageView, @DrawableRes int resId) {
        loadImage(activity, imagePath, imageView, resId, 0, 0, true);
    }

    /**
     * 从URl地址获取模糊图片
     *
     * @param imagePathUrl Image路径
     * @param imageView    ImageView控件
     * @param resId        默认图片路径
     */
    public static void getBlurImageToUrl(Activity activity, String imagePathUrl, ImageView imageView, @DrawableRes int resId) {
        if (TextUtils.isEmpty(imagePathUrl) || TextUtils.isEmpty(imagePathUrl.trim())) {
            imageView.setImageResource(resId);
        } else {
            ImagePath imagePath = new ImagePath();
            imagePath.imagePath = imagePathUrl;
            getBlurImageToUrl(activity, imagePath, imageView, resId);
        }
    }

    /**
     * 从URl地址获取模糊图片
     *
     * @param imagePath ImagePath对象
     * @param imageView ImageView控件
     * @param resId     默认图片路径
     */
    public static void getBlurImageToUrl(Activity activity, ImagePath imagePath, ImageView imageView, @DrawableRes int resId) {
        loadImage(activity, imagePath, imageView, resId, 3);
    }

//    /**
//     * 获取图片的完整路径
//     *
//     * @param imagePath 图片对象
//     */
//    public static String getImageCompletePath(ImagePath imagePath) {
//        String baseUrl;
//        if (App.isDebug()) {
//            baseUrl = Constants.DNS_ADDRESS_DEV + Constants.HTTP_APPCALL_DEV;
//        } else {
//            baseUrl = Constants.DNS_ADDRESS_ONLINE + Constants.HTTP_APPCALL_ONLINE;
//        }
//        if (imagePath != null && !imagePath.imageEmpty) {
//            return baseUrl + imagePath.imagePrefix + imagePath.imageSuffix;
//        }
//        return null;
//    }

//    /**
//     * 获取轮播图片的路径
//     *
//     * @param imagePath ImagePath对象
//     */
//    public static String getSlideViewUrl(ImagePath imagePath) {
//        if (imagePath != null && !imagePath.imageEmpty) {
//            StringBuilder stringPath = new StringBuilder();
//            if (App.isDebug()) {
//                stringPath.append(Constants.DNS_ADDRESS_DEV + Constants.HTTP_APPCALL_DEV);
//            } else {
//                stringPath.append(Constants.DNS_ADDRESS_ONLINE + Constants.HTTP_APPCALL_ONLINE);
//            }
//            stringPath.append(imagePath.imagePrefix);
//            int width = ScreenUtils.getScreenWidth();
//            stringPath.append(GLIDE_RES);
//            stringPath.append(width);
//            stringPath.append(Constants.INPUT_DASH);
//            stringPath.append((int) (width * 0.5611));
//            stringPath.append(imagePath.imageSuffix);
//            return stringPath.toString();
//        }
//        return null;
//    }

//    private static final String GLIDE_RES = "-res-";

//    public static class GlideUrlJion extends ImageSizeUrl {
//
//        String imagePrefix;
//        String imageSuffix;
//
//        public GlideUrlJion(String imagePrefix, String imageSuffix) {
//            this.imagePrefix = imagePrefix;
//            this.imageSuffix = imageSuffix;
//        }
//
//        @Override
//        public String requestCustomSizeUrl(int width, int height) {
//            StringBuilder urlBuilder = new StringBuilder();
//            if (App.isDebug()) {
//                urlBuilder.append(Constants.DNS_ADDRESS_DEV).append(Constants.HTTP_APPCALL_DEV);
//            } else {
//                urlBuilder.append(Constants.DNS_ADDRESS_ONLINE).append(Constants.HTTP_APPCALL_ONLINE);
//            }
//            urlBuilder.append(imagePrefix).append(GLIDE_RES).append(width).append(Constants.INPUT_DASH).append(height).append(imageSuffix);
//            return urlBuilder.toString();
//        }
//    }
}
